#pragma once

#include "modbus.h"
#include "cmodule.hpp"
#include "cmodule_mgr.hpp"
#include "ctimer.hpp"
#include "dp_common.h"
#include "mutex"
using namespace std;

class mbserver
{
public:
    virtual int init(){return 0;};
    public:
    int m_slaveid;
   // modbus_t *ctx = NULL; 
};

class server_tcp :public mbserver
{
public:
    virtual int init();
    int process();
    int process1();
};

class server_rtu:public mbserver
{
    public:
    virtual int init();
    int process();
};

class mbserver_mgr:public CModule
{
    public:
    void init();
    virtual void      start();
    void sort_dp();
    modbus_mapping_t* get_regmap(int slaveId);
    void handle_updatedp(dp_param_t& dpparam);
    virtual void    handle_msg(CMsg_Ptr pMsg);
    static shared_ptr<mbserver_mgr> getInstance();
    static shared_ptr<mbserver_mgr>     pdevicemgr;
    void init_dpsset_fromchannelmgr(vector<dp_param_t>dps);
    Ttimer<mstm,1>  m_loopWkTm;
    vector<dp_param_t> m_dpset;
    shared_ptr<server_tcp> m_mb_tcpserver;
    shared_ptr<server_rtu> m_mb_rtuserver;

    map<int,modbus_mapping_t*> m_regmaps; //slaveid-->map

    std::mutex m_mapLock;
};